package com.dynabook.ems.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dynabook.ems.mapper.EmsProductionRecordsMapper;
import com.dynabook.ems.mapstruct.EmsProductionRecordsConvert;
import com.dynabook.ems.model.entity.EmsMainEngine;
import com.dynabook.ems.model.entity.EmsProductionRecords;
import com.dynabook.ems.model.vo.ProductionRecordVO;
import com.dynabook.ems.service.EmsMainEngineService;
import com.dynabook.ems.service.EmsProductionRecordsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* @author tester
* @description 针对表【ems_production_records】的数据库操作Service实现
* @createDate 2025-09-16 14:53:49
*/
@Service
public class EmsProductionRecordsServiceImpl extends ServiceImpl<EmsProductionRecordsMapper, EmsProductionRecords>
    implements EmsProductionRecordsService{

    @Resource
    private EmsMainEngineService emsMainEngineService;

    @Override
    public List<ProductionRecordVO> getProductionRecords(String fixNo) {
        EmsMainEngine emsMainEngine = emsMainEngineService.getOne(new LambdaQueryWrapper<EmsMainEngine>().eq(EmsMainEngine::getFixedNo, fixNo).select(EmsMainEngine::getWorkOrderNumber));
        if(emsMainEngine == null){
            return Collections.emptyList();
        }
        if(StrUtil.isBlank(emsMainEngine.getWorkOrderNumber())){
            return Collections.emptyList();
        }
        List<EmsProductionRecords> list = this.list(new LambdaQueryWrapper<EmsProductionRecords>().eq(EmsProductionRecords::getWorkOrderNum, emsMainEngine.getWorkOrderNumber()));
       if(list == null){
           return Collections.emptyList();
       }
        List<ProductionRecordVO> productionRecordVOS = EmsProductionRecordsConvert.INSTANCE.ProductionRecordVOS(list);
        return Optional.ofNullable(productionRecordVOS).orElse(Collections.emptyList());
    }
}




